package com.zb.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.zb.entity.ZjNetwork;
import com.zb.entity.ZjWorkSpareReturn;
import com.zb.entity.ZjWorkSpareReturnReceiptSend;
import com.zb.order.service.ConWorkSpareReturnService;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkService;
import com.zb.service.ZjWorkSpareReturnReceiptSendService;
import com.zb.service.ZjWorkSpareReturnService;
import com.zb.vo.WorkSpareReturnListVo;
import com.zb.vo.WorkSpareReturnSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2023/10/30 0030
 **/
@Service
public class ConWorkSpareReturnServiceImpl implements ConWorkSpareReturnService {
    @Resource
    private ZjWorkSpareReturnService workSpareReturnService;
    @Resource
    private ZjWorkSpareReturnReceiptSendService workSpareReturnReceiptSendService;
    @Resource
    private ZjNetworkService networkService;

    // 获取工单备件旧件退回列表
    @Override
    public Map<String, Object> getList(Long custAccId, WorkSpareReturnSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<ZjWorkSpareReturn> spareReturnList = workSpareReturnService.getList(custAccId, selectVo);
        if (CollectionUtil.isEmpty(spareReturnList)) {
            return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), 0L, new ArrayList<>());
        }
        List<Long> ids = spareReturnList.stream().map(ZjWorkSpareReturn::getId).toList();
        List<Long> networkIds = spareReturnList.stream().map(ZjWorkSpareReturn::getNetworkId).distinct().toList();
        List<ZjWorkSpareReturnReceiptSend> receiptSendList = workSpareReturnReceiptSendService.listByIds(ids);
        List<ZjNetwork> networkList = networkService.listByIds(networkIds);
        // todo 退回仓库
        List<WorkSpareReturnListVo> voList = spareReturnList.stream().map(spare -> {
            WorkSpareReturnListVo vo = new WorkSpareReturnListVo();
            BeanUtil.copyProperties(spare, vo);
            vo.setOldSpareNo(spare.getCode());
            vo.setCallbackTime(spare.getReturnTime());
            vo.setNetwork(networkList.stream().filter(f -> Objects.equals(f.getId(), spare.getNetworkId())).map(ZjNetwork::getName).findFirst().orElse(""));
            vo.setCreateTime(spare.getGmtCreate());
            vo.setUpdateTime(spare.getGmtUpdate());
            ZjWorkSpareReturnReceiptSend receiptSend = receiptSendList.stream().filter(f -> Objects.equals(f.getId(), spare.getId())).findFirst().orElse(null);
            if (null != receiptSend) {
                vo.setReceiptName(receiptSend.getReceiptName());
                vo.setReceiptPhone(receiptSend.getReceiptPhone());
                vo.setReceiptProvince(receiptSend.getReceiptProvince());
                vo.setReceiptCity(receiptSend.getReceiptCity());
                vo.setReceiptArea(receiptSend.getReceiptArea());
                vo.setReceiptTown(receiptSend.getReceiptTown());
                vo.setReceiptAddress(receiptSend.getReceiptAddress());
            }
            return vo;
        }).toList();
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), workSpareReturnService.getListCount(custAccId, selectVo), voList);
    }
}
